package net.tbao.seckill.controller;

import net.tbao.common.utils.R;
import net.tbao.seckill.service.SeckillService;
import net.tbao.seckill.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class SeckillController {

    @Autowired
    SeckillService seckillService;

    /**
     * 返回当前时间可以参与秒杀的商品信息
     * @return
     */
    @ResponseBody
    @GetMapping("/currentSeckillSkus")
    public R getCurrentSeckillSkus(){
      List<SeckillSkuRedisTo> vos= seckillService.getCurrentSeckillSkus();
        return R.ok().setData(vos);
    }

    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R skuSeckillInfo(@PathVariable("skuId") Long skuId){
        SeckillSkuRedisTo to=   seckillService.skuSeckillInfo(skuId);
        return R.ok().setData(to);
    }
    @GetMapping("/kill/{killId}")
    public String seckill(@PathVariable("killId") String killId, @RequestParam("key")String key,
                          @RequestParam("num")Integer num, Model model){
       String orderSn= seckillService.seckill(killId,key,num);
       model.addAttribute("orderSn",orderSn);

       //R.ok().setData(orderSn)
        return "success";
    }
}
